package com.co.user.controller;

import com.co.comment.pojo.Comment;
import com.co.common.util.IdWorker;
import com.co.user.service.CommentService;
import com.co.common.entity.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private IdWorker idWorker;


    /**
     * ZhComment分页条件搜索实现
     *
     * @param comment
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Comment>> findPage(@RequestBody(required = false) Comment comment, @PathVariable int page, @PathVariable int size) {
        //调用ZhCommentService实现分页条件查询ZhComment
        PageInfo<Comment> pageInfo = commentService.findPage(comment, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * ZhComment分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Comment>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用ZhCommentService实现分页查询ZhComment
        PageInfo<Comment> pageInfo = commentService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param comment
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Comment>> findList(@RequestBody(required = false) Comment comment) {
        //调用ZhCommentService实现条件查询ZhComment
        List<Comment> list = commentService.findList(comment);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用ZhCommentService实现根据主键删除
        commentService.delete(id);
        return Result.ok();
    }

    /**
     * 修改ZhComment数据
     *
     * @param comment
     * @param id
     * @return
     */
//    @PutMapping(value = "/{id}")
    public Result<?> update(@RequestBody Comment comment, @PathVariable String id) {
        //设置主键值
        comment.setId(id);
        //调用ZhCommentService实现修改ZhComment
        commentService.update(comment);
        return Result.ok();
    }

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        comment.setId(idWorker.nextId() + "");
        comment.setCreateTime(new Date());
        comment.setDislike(0);
        comment.setLike(0);
        comment.setUpdateTime(new Date());
        commentService.add(comment);
        return Result.ok("添加评论成功");
    }

    /**
     * 根据ID查询ZhComment数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Comment> findById(@PathVariable String id) {
        //调用ZhCommentService实现根据主键查询ZhComment
        Comment comment = commentService.findById(id);
        return Result.ok(comment);
    }

    /**
     * 查询ZhComment全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Comment>> findAll() {
        //调用ZhCommentService实现查询所有ZhComment
        List<Comment> list = commentService.findAll();
        return Result.ok(list);
    }
}
